<?php

namespace App\Http\Controllers\Admin\Category;

use App\Models\Category\Category;
use App\Services\CategoryService;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ProductCategoryController extends BaseController
{
    /**
     * 分类的组名
     */
    public $type = 'product';


    /**
     * 状态修改
     */
    public function status(Request $request)
    {
        $this->validateData($request);

        $category_id = $request->input('id');

        $category = Category::query()->where('category_group_id',$this->categoryService->category_group_id)->findOrFail($category_id);

        $level = $category->level;

        $category->when($level == 1,function ($q) use ($category_id,$request){

            $q->where('top_id',$category_id)->update(['status'=>$request->input('status')]);

        })->when($level == 2,function ($q) use ($category_id,$request,$category){

            $q->where('pid',$category_id)->update(['status'=>$request->input('status')]);
            $category->where('id',$category_id)->update(['status'=>$request->input('status')]);

        })->when($level == 3,function ($q) use ($category_id,$request){

            $q->where('id',$category_id)->update(['status'=>$request->input('status')]);

        });

        return $this->response->noContent();
    }

    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        $categoryService = new CategoryService(request(), $this->type);

        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [
                    //bail在首次验证失败后立即终止验证。
                    'name' => ['bail', 'nullable', 'string', 'max:32', function ($attr, $value, $fail) use ($categoryService) {
                        $result = $categoryService->checkCategoryName(request()->input('pid', null), $value);
                        if (!$result) {
                            return $fail('分类名name重复');
                        }
                    }],
                    'nickname' => ['bail', 'required', 'string', 'max:50', function ($attr, $value, $fail) use ($categoryService) {
                        //属性的名称、属性的值以及$fail验证失败时应调用的回调
                        $result = $categoryService->checkCategoryNickname(request()->input('pid', null), $value);

                        if (!$result) {
                            return $fail('分类名nickname重复');
                        }
                    }],
                    'pid' => 'nullable|integer'
                ];
                break;
            case 'PATCH':
                //从服务器中拿到id
                $id = route_parameter($this->type.'_category');

                if (empty($id)){
                    $id = route_parameter('id');
                }

                $category = $categoryService->getCacheCategory($id);

                if (empty($category)) {
                    throw new HttpException(404,'ID或路由错误');
                }

                return [
                    'name' => ['bail', 'nullable', 'string', 'max:32', function ($attr, $value, $fail) use ($categoryService,$id) {
                        $result = $categoryService->checkCategoryName(request()->input('pid', null), $value, $id);
                        if (!$result) {
                            return $fail('分类昵称name重复');
                        }
                    }],
                    'nickname' => ['bail', 'required', 'string', 'max:50', function ($attr, $value, $fail) use ($categoryService,$id) {
                        $result = $categoryService->checkCategoryNickname(request()->input('pid', null), $value, $id);
                        if (!$result) {
                            return $fail('分类名nickname重复');
                        }
                    }],
                    'pid' => 'nullable|integer'
                ];
                break;
            case 'PUT':
                return [
                    'id' => 'bail|required',
                    'status' => 'bail|required'
                ];
                break;
        }
        return [];
    }
}
